package com.iyao_app.im.adapter;

import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.IMUser;
import com.iyao_app.util.ResourcesUtils;
import com.iyao_app.util.TimeFormat;
import com.iyao_app.util.UserUtils;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.netease.nimlib.sdk.msg.model.RecentContact;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tongxue on 2017/12/16.
 */

public class IMMessageListAdapter extends BaseQuickAdapter<RecentContact, IMMessageListAdapter.ViewHolder> {

    private static List<IMMessage> emMessageList = new ArrayList<IMMessage>();

    public IMMessageListAdapter(@Nullable List<RecentContact> data) {
        super(R.layout.list_item_conversation, data);
    }

    @Override
    protected void convert(ViewHolder helper, RecentContact item) {

        IMUser user = UserUtils.getUser(item.getContactId());
        if (user != null) {
            if (TextUtils.isEmpty(user.head)) {
                helper.mIvAvatar.setImageResource(R.drawable.ic_guanlogo);
            } else {
                Uri uri2 = Uri.parse(user.head);
                ImageRequest request2 = ImageRequestBuilder.newBuilderWithSource(uri2)
                        .setResizeOptions(new ResizeOptions((int) ResourcesUtils.getDimension(R.dimen.x80), (int) ResourcesUtils.getDimension(R.dimen.x80)))
                        .build();
                PipelineDraweeController controller2 = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(helper.mIvAvatar.getController())
                        .setImageRequest(request2)
                        .build();
                helper.mIvAvatar.setController(controller2);
            }

            helper.mTvUsername.setText(user.nickName);
        } else {
            helper.mIvAvatar.setImageResource(R.drawable.ic_guanlogo);
            helper.mTvUsername.setText("");
        }

        helper.mTvMsg.setText(item.getContent());
        helper.mTvTime.setText(TimeFormat.getTime(item.getTime()));
        if (item.getUnreadCount() != 0) {
            helper.mTvUnread.setText(String.valueOf(item.getUnreadCount()));
        } else {
            helper.mTvUnread.setVisibility(View.GONE);
        }
    }


    static class ViewHolder extends BaseViewHolder {

        @BindView(R.id.iv_avatar)
        SimpleDraweeView mIvAvatar;
        @BindView(R.id.tv_username)
        TextView mTvUsername;
        @BindView(R.id.tv_msg)
        TextView mTvMsg;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.tvUnread)
        TextView mTvUnread;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
